package com.sky.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/*
 * 套餐管理
 */
@RestController("adminsetmealcontroller")
@RequestMapping("/admin/setmeal")
@Api(tags="套餐相关接口")
@Slf4j
public class SetmealController {
	//创建service对象
	@Autowired
	SetmealService setmealservice;
	
	 @PostMapping
	 @ApiOperation("新增套餐")
	 @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//key: setmealCache::100
	public Result InsertSetMeal(@RequestBody SetmealDTO setmealdto) throws JsonProcessingException
	{
		//记录日志
		 
		log.info("新增套餐，参数：{}",setmealdto);
		 log.info("接收到的JSON数据: {}", new ObjectMapper().writeValueAsString(setmealdto));
		//调用service进行封装实现
		setmealservice.InsertSetMeal(setmealdto);
		return Result.success();
	}
	 /*
	  * 分页查询
	  * 
	  */
	 @GetMapping("/page")
	 @ApiOperation("套餐分页查询")
	 public Result<PageResult> page(SetmealPageQueryDTO setpagequerydto)
	 {
		 //日志
		 log.info("套餐分页查询的参数：{}",setpagequerydto);
		 //调用service，获取想要的，封装后的数据返回给web端
		 PageResult pageresult =  setmealservice.page(setpagequerydto);
		 
		 return Result.success(pageresult);
	 }
	 /*
	  * 修改套餐
	  * 1、回显
	  * 2、修改
	  */
	 //回显
	 @GetMapping("/{id}")
	 @ApiOperation("套餐的回显")
	 public Result<SetmealVO> GetById(@PathVariable Long id)
	 {
		 //获取id的数据
		 log.info("参数回显：{}",id);
		 //调用service获取id的数据
		 SetmealVO setmealvo = setmealservice.GetById(id);
		 return Result.success(setmealvo);
	 }
	/*
	 * 更新内容
	 * 1、删除一些原本的如：dishs
	 * 2、更新分为setmeal和setmealdish
	 * 
	 */
	 @PutMapping
	 @ApiOperation("修改套餐")
	 @CacheEvict(cacheNames = "setmealCache",allEntries = true)
	 public Result update(@RequestBody SetmealDTO setmealdto)
	 {
		 //记录日志
		 log.info("修改套餐的参数：{}",setmealdto);
		 //调用service
		 setmealservice.update(setmealdto);
		 return Result.success();
	 }
	 /*
	  * 删除套餐
	  * 
	  */
	 @DeleteMapping
	 @ApiOperation("批量删除套餐")
	 @CacheEvict(cacheNames = "setmealCache",allEntries = true)
	 public Result deletedishs(@RequestParam List<Long>ids)
	 {
		 log.info("套餐删除的参数：{}",ids);
		 setmealservice.deletedishs(ids);
		 return Result.success();
	 }
	 /*
	  * 套餐的启售和禁售
	  */
	 @PostMapping("/status/{status}")
	 @ApiOperation("套餐的启售和禁售")
	 @CacheEvict(cacheNames = "setmealCache",allEntries = true)
	 public Result StartOrStop(@PathVariable Integer status,@RequestParam Long id)
	 {
		 //记录日志
		 log.info("套餐的起售和禁售的参数：{},{}",status,id);
		 //调用service进行处理
		 setmealservice.StartOrStop(status,id);
		 return Result.success();
	 }
}
